package cm.tfd.hospital.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Map;

import cm.tfd.hospital.ui.activity.BaseActivity;
import okhttp3.Call;


/**
 * Created by tianfeng on 2018/3/25.
 * 所有fragment基类
 */

public abstract class BaseFragment extends Fragment {
    protected Activity mActivity;
    protected String TAG;
    protected SharedPreferences sp;
    protected View mRootView;
    protected boolean mIsInitialized;
    protected boolean mIsDestroyed;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        TAG = this.getClass().getSimpleName();
        mActivity = getActivity();
//        sp = context.getSharedPreferences(Common.SP_NAME, MODE_PRIVATE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mIsInitialized = true;
        mIsDestroyed = false;
        if (mRootView == null) {
            mRootView = initView(savedInstanceState);
        } else {
            ViewGroup localViewGroup = (ViewGroup) mRootView.getParent();
            if (localViewGroup != null) {
                localViewGroup.removeView(mRootView);
            }
        }
        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 判断当前fragment是否显示
        if (getUserVisibleHint()) {
            if (mIsInitialized) {
                mIsInitialized = false;
                // 加载各种数据
                onLazyLoad();
            }
        }
    }

    /**
     * 首次可见时回调加载数据，用于延迟加载
     */
    protected void onLazyLoad() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsDestroyed = true;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // 每次切换fragment时调用的方法
        if (isVisibleToUser) {
            if (mIsInitialized) {
                mIsInitialized = false;
                // 加载各种数据
                onLazyLoad();
            }
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(savedInstanceState);
    }

    protected void initData(Bundle savedInstanceState) {

    }

    protected abstract View initView(Bundle savedInstanceState);


    protected void onPostError(Call call, Exception e, int requestCode) {

    }

    protected void onPostResponse(String response, int requestCode) {

    }

    public void showLoading() {
        if (getActivity() != null) {
            ((BaseActivity) getActivity()).showLoading();
        }
    }

    public void dismissLoading() {
        if (getActivity() != null) {
            ((BaseActivity) getActivity()).dismissLoading();
        }
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
    public void showMsg(String msg){
        Toast.makeText(getActivity(),msg, Toast.LENGTH_SHORT).show();
    }

    /**
     *
     * 本地保存账号密码
     * @param context
     * @param
     * @param
     */
    public static void saveLoginInfo(Context context, String access_token, String refresh_token
    ) {
        // 获取SharedPreferences对象
        SharedPreferences sharedPre = context.getSharedPreferences("config",
                context.MODE_PRIVATE);
        // 获取Editor对象
        SharedPreferences.Editor editor = sharedPre.edit();
        // 设置参数
        editor.putString("access_token", access_token);
        editor.putString("refresh_token", refresh_token);

        // 提交
        editor.commit();
    }
}
